<template>
  <div class="coupon">
    <div class="header">
      <h3 class="cll_h3">优惠券管理</h3> <button @click="handlepush">新增</button>
    </div>
    <hr>
    <div class="formbox">
      <input type="text" placeholder="优惠券名称" v-model.trim="form.mes"> <button @click="couponsearch">搜索</button>
    </div>
    <hr class="hr2">
    <!-- 表格展示 -->
    <div class="tablebox">
      <el-table ref="multipleTableRef" :data="couponlist" style="width: 100%" :header-row-style="{ color: '#000' }">
        <el-table-column label="ID" width="100">
          <template #default="scope">{{ scope.row.id }}</template>
        </el-table-column>
        <el-table-column property="couponname" label="优惠券名称" width="160" />
        <el-table-column property="scope" label="指定范围" show-overflow-tooltip width="150" />
        <el-table-column property="condition" label="使用条件" show-overflow-tooltip width="150" />
        <el-table-column property="sum" label="金额" show-overflow-tooltip width="130" />
        <el-table-column property="start" label="启用状态" show-overflow-tooltip width="150" />
        <el-table-column property="ndoseame" label="发放总量" show-overflow-tooltip width="150" />
        <el-table-column property="startTime" label="开始时间" show-overflow-tooltip width="150" />
        <el-table-column label="操作" width="150" align="center">
          <template #default="scope">
            <span class="audit" @click="handlechange(scope.row)">编辑</span>
            <span class="audit" @click="hangdleclear(scope.row.id)">删除</span>
          </template>
        </el-table-column>

      </el-table>
      <div class="pagebox">
        <el-pagination background layout="prev, pager, next" :total="total" :default-page-size="pageSize"
          @current-change="pagechange" />
      </div>
    </div>
  </div>
</template>
<script lang="ts">
import { defineComponent } from 'vue';
import { getcouponlist } from '@/api/coupon'

export default defineComponent({
  name: 'coupon',
  data() {
    return {
      couponlist: [
        {
          id: '',
          couponname: '20元优惠券',
          scope: '男士餐可用',
          condition: '满100可用',
          sum: '20',
          start: '是',
          ndoseame: 200,
          startTime: '',
        },
      ],
      total: 5,
      pageSize: 5,
      form: {
        mes: '',
        page: 1,
      }

    }
  },
  created() {
    this.getList()
  },
  methods: {
    // 跳转新增页面
    handlepush() {
      this.$router.push('/coupon/pushcoupon')
    },
    // 删除信息
    hangdleclear(id: any) {
      this.couponlist = this.couponlist.filter(item => item.id !== id)
      console.log('删除', this.couponlist)

    },
    // 跳转编辑页面
    handlechange(item: any) {
      this.$router.push({
        path: '/coupon/pushcoupon',
        query: item
      })
    },
    // 搜索事件
    async couponsearch() {
      const data = this.form
      // @ts-ignore
      const res = await getcouponlist(data)
      // @ts-ignore
      this.couponlist = res.list
      // @ts-ignore
      this.total = res.total
      // @ts-ignore
      this.pageSize = res.pageSize
    },
    // 分页器
    pagechange(page: number) {
      this.getList(page)
      
    },
    async getList(page=1) {
      const data = this.form
      data.page = page
      const res = await getcouponlist(data)
      //@ts-ignore
      const { list, total, pageSize } = res
      this.couponlist = list
      this.total = total
      this.pageSize = pageSize
    }
  },
})
</script>

<style scoped lang="less">
// 原生html的样式
.header {
  display: flex;
  justify-content: space-between;
  padding-right: 30px;

  button {
    width: 94px;
    height: 31px;
    cursor: pointer;
    background-color: #ccc;
    border: none;
  }

  ;
}

.formbox {
  padding-left: 20px;

  input {
    width: 277px;
    height: 25px;
  }

  ;

  button {
    width: 94px;
    height: 31px;
    border: none;
    background-color: #999;
    color: #fff;
    margin-left: 40px;
    cursor: pointer;
  }

  ;
}

.tablebox {
  padding-left: 30px;
}

.hr2 {
  margin: 30px 0 10px 0;
}

.audit {
  margin: 0 15px;
  cursor: pointer;
}

.pagebox {
  display: flex;
  justify-content: center;
}
</style>
